package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author: Zheng
 * Date:   2024/2/20  11:45
 * Package：com.sky.service.impl
 * Description：处理套餐业务逻辑
 * Version：1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * @param setmealDTO
     * @description： 新增套餐
     * @date：2024/2/20 11:54
     * @return：
     */
    @Override
    @Transactional //开启事务
    public void add(SetmealDTO setmealDTO) {
        //构造套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //将套餐数据插入到setmeal表中
        setmealMapper.insert(setmeal);

        //构造套餐菜品关联数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //Long setmealId = setmeal.getId();

        setmealDishes.forEach(sd -> {
            //SetmealDish setmealDish = SetmealDish.builder()
            //        .dishId(sd.getDishId())
            //        .setmealId(setmeal.getId())
            //        .name(sd.getName())
            //        .price(sd.getPrice())
            //        .copies(sd.getCopies())
            //        .build();
            //setmealDishMapper.insert(setmealDish);
            //关联套餐id
            sd.setSetmealId(setmeal.getId());
        });
        //将套餐菜品关联数据保存到菜品关系表setmeal_dish中
        setmealDishMapper.insertBatch(setmealDishes);


    }

    /**
     * @param setmealPageQueryDTO
     * @description： 套餐分页查询
     * @date：2024/2/20 15:15
     * @return：
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageMethod.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealPage = setmealMapper.page(setmealPageQueryDTO);

        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    /**
     * @param id
     * @description： 根据id查询套餐，回显数据
     * @date：2024/2/20 16:39
     * @return：
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        //SetmealVO setmealVO = new SetmealVO();
        //Setmeal setmeal = setmealMapper.getById(id);
        //BeanUtils.copyProperties(setmeal, setmealVO);
        //
        //List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
        //setmealVO.setSetmealDishes(setmealDishes);
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        return setmealVO;
    }

    /**
     * @param setmealDTO
     * @description： 套餐修改
     * @date：2024/2/20 15:52
     * @return：
     */
    @Override
    @Transactional //开启事务
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //修改套餐表数据
        setmealMapper.update(setmeal);

        //套餐id
        Long setmealId = setmealDTO.getId();
        //删除套餐和菜品关联数据
        setmealDishMapper.deleteBySetmealId(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(sd -> {
            //SetmealDish setmealDish = SetmealDish.builder()
            //        .copies(sd.getCopies())
            //        .dishId(sd.getDishId())
            //        .price(sd.getPrice())
            //        .name(sd.getName())
            //        .setmealId(sd.getSetmealId())
            //        .build();
            //setmealDishMapper.update(setmealDish);
            sd.setSetmealId(setmealId);
        });
        //重新插入套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * @param ids
     * @description： 批量删除
     * @date：2024/2/20 18:13
     * @return：
     */
    @Override
    @Transactional //开启事务
    public void deleteBatch(List<Long> ids) {
        //起售中的套餐不能删除
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);

            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //删除套餐的同时，需要将套餐菜品关系数据删除
        ids.forEach(setmealId -> {
            //删除套餐表中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
    }

        /**
         * @param status
         * @param id
         * @description： 套餐起售停售
         * @date：2024/2/20 19:44
         * @return：
         */
        @Override
        public void startOrStop(Integer status, Long id) {

            //起售套餐时，判断套餐内是否有停售套餐，有停售
            if (status.equals(StatusConstant.ENABLE)) {
                List<Dish> dishList = dishMapper.getBySeatmealId(id);
                if (!dishList.isEmpty()) {
                    dishList.forEach(dish -> {
                        if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                        }
                    });

                }
            }
            Setmeal setmeal = Setmeal.builder()
                    .status(status)
                    .id(id)
                    .build();
            setmealMapper.update(setmeal);
        }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
